package com.whut.monitor.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 系统工具、获取系统的一些信息
 * @author Sandeepin
 *         2017/11/9 0009
 */
public class SystemUtil {

    /**
     * 获取本地Mac地址，不用-分隔
     * @return mac字符串
     * @throws SocketException
     * @throws UnknownHostException
     */
    public static String getLocalMac() throws SocketException, UnknownHostException {
        InetAddress ia = InetAddress.getLocalHost();
        //获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
//        System.out.println("mac数组长度："+mac.length);
        StringBuilder sb = new StringBuilder("");
        for(int i=0; i<mac.length; i++) {
            if(i!=0) {
//                sb.append("-");
                // mac地址不用-号分割
                sb.append("");
            }
            //字节转换为整数
            int temp = mac[i]&0xff;
            String str = Integer.toHexString(temp);
//            System.out.println("每8位:"+str);
            if(str.length()==1) {
                sb.append("0").append(str);
            }else {
                sb.append(str);
            }
        }
//        System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
        return sb.toString().toUpperCase();
    }

    /**
     * 获取一个String的md5值
     *
     * @param str
     * @return
     */
    public static String getMd5(String str) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bs = md5.digest(str.getBytes());
        StringBuilder sb = new StringBuilder(40);
        for(byte x:bs) {
            if((x & 0xff)>>4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    // 快速切片 字符串
    public static List<String> fastSplit(String str){
        List<String> out = new ArrayList<>();
        StringTokenizer st = new StringTokenizer(str, ",");
        while(st.hasMoreTokens()){
            out.add(st.nextToken());
        }
        return out;
    }

    // 快速切片 二维
    public static String[][] fastSplit(List<String> textList){
        if (textList.size()>0) {
            String[] line = textList.get(0).split(",");
            int row = textList.size();
            int col = line.length;
            String[][] mat = new String[row][col];
            for (int i = 0; i < row; i++) {
                StringTokenizer st = new StringTokenizer(textList.get(i), ",");
                int j=0;
                while(st.hasMoreTokens()){
                    mat[i][j++] = st.nextToken();
                }
            }
            return mat;
        }
        return null;
    }

//    public static void main(String[] args) throws NoSuchAlgorithmException {
////        List<String> out = fastSplit("1,,,,5,");
//        List<String> textList = new ArrayList<>();
//        textList.add("1,2,,,,5,");
//        textList.add("6,7,,,,0,");
//        String[][] A = fastSplit(textList);
//        System.out.println(getMd5("sandeepin"));
//    }
}
